//
//  UIControl+Click.m
//  Preventdoubleclicking
//
//  Created by 红嘴欧 on 2018/6/28.
//  Copyright © 2018年 person.qing. All rights reserved.
//

#import "UIControl+Click.h"
#import <objc/runtime.h>

@implementation UIControl (Click)

- (NSTimeInterval)responseInterval {
    return [objc_getAssociatedObject(self, @selector(responseInterval)) doubleValue];
}

- (void)setResponseInterval:(NSTimeInterval)responseInterval {
    objc_setAssociatedObject(self, @selector(responseInterval), @(responseInterval), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)isIgnore {
    return [objc_getAssociatedObject(self, @selector(isIgnore)) boolValue];
}

- (void)setIsIgnore:(BOOL)isIgnore {
    objc_setAssociatedObject(self, @selector(isIgnore), @(isIgnore), OBJC_ASSOCIATION_ASSIGN);
}

+ (void)load {
    Method A = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    Method B = class_getInstanceMethod(self, @selector(my_sendAction:to:forEvent:));
    method_exchangeImplementations(A, B);
}

- (void)my_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    if (self.isIgnore) {
        return;
    }
    if (self.responseInterval>0) {
        self.isIgnore = YES;
        
        [self performSelector:@selector(setIsIgnore:) withObject:@(NO) afterDelay:self.responseInterval];
    }
    [self my_sendAction:action to:target forEvent:event];
}

@end
